package com.ftg.learn.chapter04.question;

/**
 * @author 布衣
 */
//外部的java
public class Leetcode {

    //成员内部类
    public class ListNode {
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode reverseList(ListNode head) {
        //临界
        if (head == null) {
            return null;
        }
        //前置域  next pre
        ListNode pre = null;
//        [1,2,3]
        while (head != null) {
            // 3   next null
            ListNode next = head.next;
            // 1.next = null;[1] [2,3]  [2,1]
//            [3,2,1]
            head.next = pre;
            //pre = head  pre = [1]  pre = [2,1]  pre =  [3,2,1]
            pre = head;
            // head = next  head [2,3]    head [3] head = null
            head = next;
        }
        return pre;
    }

    public static void main(String[] args) {

        Leetcode leetcode = new Leetcode();

        ListNode l1 = leetcode.new ListNode(1);
        ListNode l2 = leetcode.new ListNode(2);
        ListNode l3 = leetcode.new ListNode(3);

        l1.next = l2;
        l2.next = l3;

        leetcode.reverseList(l1);
    }

}
